package com.qf.manager.web;

import com.qf.common.fdfs.FastDFSFile;
import com.qf.common.fdfs.FastDFSUtils;
import com.qf.common.util.JsonUtils;
import com.qf.common.util.PropKit;
import com.qf.common.util.StrKit;
import com.qf.manager.pojo.dto.DownloadResult;
import com.qf.manager.pojo.dto.Page;
import com.qf.manager.pojo.po.Drug;
import com.qf.manager.pojo.po.Drugcat;
import com.qf.manager.pojo.vo.DrugCustem;
import com.qf.manager.pojo.vo.DrugQuery;
import com.qf.manager.service.ManagerDrugService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/5/25.
 */
@Controller
public class ManagerDrugAction {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ManagerDrugService managerDrugService;

    @ResponseBody
    @RequestMapping(value = "/drugManagement", method = RequestMethod.GET)
    public DownloadResult<DrugCustem> listItemsByPage(Page page,DrugQuery query,HttpSession session) {
        DownloadResult<DrugCustem> list = null;
        try {
            list = managerDrugService.listDrug(page,query);
            List<Drugcat> dc = managerDrugService.listDrugcat();
            session.setAttribute("dc",dc);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "/batch",method = RequestMethod.POST)
    public int updateStatusByIds(@RequestParam("ids[]") List<Long> ids){

        int i=0;
        try {
            i = managerDrugService.updateStatusByIds(ids);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return i;
    }

    @RequestMapping(value = "/addDrug",method = RequestMethod.POST)
     public void addDrug(Drug d){
        d.setDstatus(1);
        managerDrugService.addDrug(d);

    }

    @RequestMapping(value = "/drugedit",method = RequestMethod.GET)
    public void drugEdit(@RequestParam("drugid") Long drugid,Model model){
        Drug d = managerDrugService.queryDrug(drugid);
        System.out.print(d);
        model.addAttribute("d",d);

    }

    @RequestMapping(value = "/editDrug",method = RequestMethod.POST)
    public void editDrug(Drug d){
        System.out.println(d);
        managerDrugService.editDrug(d);
    }

    @RequestMapping(value = "/updateDstatus",method = RequestMethod.POST)
    public void updateDstatus(Long drugid){
        managerDrugService.updateDstatus(drugid);
    }

    @ResponseBody
    @RequestMapping(value = "/uploadImage",method = RequestMethod.POST)
    public String uploadImage(@RequestParam MultipartFile file,HttpServletRequest request,HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        String jsonString = "";
        try{
            FastDFSFile fastDFSFile = new FastDFSFile(file.getBytes(),file.getOriginalFilename(),file.getSize());
            String filePath = FastDFSUtils.uploadFile(fastDFSFile);
            String basePath = PropKit.use("fdfs_client.conf").get("fdfs_server");
            if(StrKit.notBlank(filePath)){
                map.put("code",0);
                map.put("msg","success");
                Map<String,Object> dataMap = new HashMap<String,Object>();
                dataMap.put("src",basePath+"/"+filePath);
                map.put("data",dataMap);
            }else{
                map.put("code",1);
                map.put("msg","filed");
                Map<String,Object> dataMap = new HashMap<String,Object>();
                dataMap.put("src","");
                map.put("data",dataMap);
            }
            String drugImg = basePath+"/"+filePath;
            session.setAttribute("drugImg",drugImg);
            jsonString = JsonUtils.objectToJson(map);
        }catch (Exception e){e.printStackTrace();}
        return jsonString;
    }
}
